<?php

class MembersController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {
		if(!$_SESSION['member_info']['not_from_wechat'] && !$_SESSION['member_info']['open_id'])
		{
			$this->_redirect("/?redirect_uri=".$_SERVER['REQUEST_URI']);
		}
		
		if(!$_SESSION['init'])
		{
			$mod = new Sys_Init();
			$mod->LoadBaseInfoToSession();
		}
		
		$this->view->layout = "front";
    }

	function indexAction()
	{
		if($_SESSION['member_info']['cellphone'])
		{
			$params = $this->_request->getParams();	
			$this->view->title = "会员中心";

			$url = "http://wap.lingcaibao.com/mobile/channel/loginJump";
			$data_array = array(
				'username' => $_SESSION['member_info']['cellphone']
			);

			$mod = new Sys_LcbApi();
			$this->view->lcb_url = $mod->CallServerGet($url, $data_array);
		}else{
			$this->_redirect("/members/login");
		}
	}
	
	function profileAction()
	{
		if($_SESSION['member_info']['cellphone'])
		{
			$params = $this->_request->getParams();	
			$this->view->title = "个人资料";

			if($_SESSION['member_info']['member_id'])
			{
				$mod_members = new Db_Tables_Members();
				$row = $mod_members->fetchRow("member_id='".$_SESSION['member_info']['member_id']."'");

				if($row)
				{
					$this->view->info = $row;
				}else{
					$this->_redirect("/");
				}
			}else{
				$this->_redirect("/");
			}

			if(1 == $params['success'])
			{
				$this->view->news = "更改成功！";
			}
		}else{
			$this->_redirect("/members/login");
		}
	}
	
	function profileEditAction()
	{
		if($_SESSION['member_info']['cellphone'])
		{
			$params = $this->_request->getParams();	
			$this->view->title = "资料更新";

			if($_SESSION['member_info']['member_id'])
			{
				$mod_members = new Db_Tables_Members();
				$row = $mod_members->fetchRow("member_id='".$_SESSION['member_info']['member_id']."'");

				if($row)
				{
					$this->view->info = $row;
				}else{
					$this->_redirect("/");
				}
			}else{
				$this->_redirect("/");
			}

			if(1 == $params['error'])
			{
				$this->view->news = "昵称已存在，请更换";
			}elseif(2 == $params['error'])
			{
				$this->view->news = "数据保存失败";
			}
		}else{
			$this->_redirect("/members/login");
		}
	}
	
	function profileEditSubmitAction()
	{
		$params = $this->_request->getParams();	
		
		if($_SESSION['member_info']['member_id'])
		{
			$mod_members = new Db_Tables_Members();
			$row = $mod_members->fetchRow("member_id='".$_SESSION['member_info']['member_id']."'");
			
			if($row)
			{
				if($params['nick_name'])
				{
					$check_nickname = $mod_members->fetchRow("nick_name='".trim($params['nick_name'])."' and member_id != '".$_SESSION['member_info']['member_id']."'");
					
					if(!$check_nickname)
					{
						if($params['cellphone'])
						{
							$row->cellphone = trim($params['cellphone']);
						}
						
						if($params['nick_name'])
						{
							$row->nick_name = trim($params['nick_name']);
						}
						
						if($params['email'])
						{
							$row->email = trim($params['email']);
						}
						
						if(is_numeric($params['gender']))
						{
							$row->gender = trim($params['gender']);
						}
						
						if($params['passwd1'] && $params['passwd2'])
						{
							$row->password = sha1($params['passwd1']);
						}
						
						if($row->save())
						{
							$this->_redirect("/members/profile/success/1");
						}else{
							$this->_redirect("/members/profile-edit/error/2"); //数据保存失败
						}
					}else{
						$this->_redirect("/members/profile-edit/error/1"); //昵称已存在
					}
				}
			}else{
				$this->_redirect("/");
			}
		}else{
			$this->_redirect("/");
		}
	}
	
	function registerAction()
	{
		$params = $this->_request->getParams();	
		$this->view->title = "会员注册";
		
		if($params['level3_id'])
		{
			$this->view->level3_id = $params['level3_id'];
		}
		
		if($params['error'])
		{
			switch($params['error'])
			{
				case 1:
					$this->view->news = "数据写入失败";
					break;
				case 2:
					$this->view->news = "手机号已注册，请直接<a href='/members/login'>[登录]</a>或更换手机号";
					break;
				case 3:
					$this->view->news = "必要信息缺失";
					break;
				case 4:
					$this->view->news = "远端会员注册失败";
					break;
			}
		}
		
		//create vcode
		$mod_veri = new Db_Tables_CellphoneVerification();
		$vcode = $mod_veri->AddVcode($params['cellphone']);
		
		//send sms $vcode to $params['cellphone']
		$mod_sms = new Sys_Extensions();
		$this->view->send_result = $mod_sms->SendVerifyCode($params['cellphone'], $vcode);			
		$this->view->cellphone = $params['cellphone'];
	}

	function sendVerifyCodeAction()
	{	
		$params = $this->_request->getParams();
		$cellphone = $params['cellphone'];
		//create vcode
		$mod_veri = new Db_Tables_CellphoneVerification();
		$vcode = $mod_veri->AddVcode($cellphone);
			
		//send sms $vcode to $params['cellphone']
		$mod_sms = new Sys_Extensions();
		$send_result = $mod_sms->SendVerifyCode($cellphone, $vcode);
		echo $send_result = json_encode($send_result);
		//var_dump($send_result);
		die;			
		
	}
	
	function registerSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['cellphone'] && $params['passwd1'])
		{
			$mod_members = new Db_Tables_Members();
			
			$check_exist = $mod_members->fetchRow("cellphone='".trim($params['cellphone'])."'");
			
			if(!$check_exist)
			{
				$row = $mod_members->createRow();
				$row->cellphone = trim($params['cellphone']);
				$row->nick_name = trim($params['nick_name']);
				$row->password = sha1(trim($params['passwd1']));

				if($params['gender'])
				{
					$gender = $params['gender'];
				}else{
					$gender = 0;
				}
				$row->gender = $gender;

//				if($params['email'])
//				{
//					$row->email = trim($params['email']);
//				}

				$row->ctime = date("Y-m-d H:i:s");

				$member_id = $row->save();

				if($member_id)
				{
					//register in lcb
					$lcb_url = "http://www.lingcaibao.com/channelpub/register";
					
					$data_array = array(
						'username' => trim($params['cellphone']),
						'userType' => '1'
					);

					$lcb = new Sys_LcbApi();
					$result = $lcb->CallServer($lcb_url, $data_array);
					
					if($result['result'])
					{
						$mod_members->member_id = $member_id;
						$mod_members->LoadMemberInfoToSession();

						if($_SESSION['member_info']['open_id'])
						{
							$mod_wechat = new Db_Tables_MembersWechat();
							$mod_wechat->member_id = $check_exist->member_id;
							$mod_wechat->open_id = $_SESSION['member_info']['open_id'];
							$mod_wechat->AddRecord();
						}

						if($params['level3_id'])
						{
							$this->_redirect("/index/level3/level_id/".$params['level3_id']);
						}else{
							$this->_redirect("/members/index");
						}
					}else{
						$this->_redirect("/members/register/error/4"); //远端会员注册失败
					}
				}else{
					$this->_redirect("/members/register/error/1"); //数据写入失败
				}
			}else{
				$this->_redirect("/members/register/error/2"); //手机号已注册
			}
		}else{
			$this->_redirect("/members/register/error/3"); //必要信息缺失
		}
	}
	
	function loginAction()
	{
		$params = $this->_request->getParams();	
		$this->view->title = "登陆";
		
		if($params['level3_id'])
		{
			$this->view->news = "请先登陆或注册为会员再参与抽奖";
			$this->view->level3_id = $params['level3_id'];
		}
		
		if($params['error'])
		{
			switch($params['error'])
			{
				case 1:
					$this->view->news = "手机号或密码有误";
					break;
				case 2:
					$this->view->news = "必要信息缺失";
					break;
			}
		}
	}
	
	function loginSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['cellphone'] && $params['passwd'])
		{
			$mod_members = new Db_Tables_Members();
			
			$check_exist = $mod_members->fetchRow("cellphone='".trim($params['cellphone'])."' and password='".sha1(trim($params['passwd']))."'");
			
			if($check_exist)
			{
				$mod_members->member_id = $check_exist->member_id;
				$mod_members->LoadMemberInfoToSession();
				
				if($_SESSION['member_info']['open_id'])
				{
					$mod_wechat = new Db_Tables_MembersWechat();
					$mod_wechat->member_id = $check_exist->member_id;
					$mod_wechat->open_id = $_SESSION['member_info']['open_id'];
					$mod_wechat->AddRecord();
				}
				
				
				if($params['level3_id'])
				{
					$this->_redirect("/index/level3/level_id/".$params['level3_id']);
				}else{
					$this->_redirect("/members/index");
				}
			}else{
				$this->_redirect("/members/login/error/1"); //手机号或密码错
			}
		}else{
			$this->_redirect("/members/login/error/2"); //必要信息缺失
		}
	}
	
	function lcbLoginAction()
	{
		$mod = new Sys_LcbApi();
		
		$mod->CallServerRedirect();
	}
	
	function logoutAction()
	{
		//remove open_id
		if($_SESSION['member_info']['member_id'])
		{
			$mod_members_wechat = new Db_Tables_MembersWechat();
			$mod_members_wechat->delete("member_id=".$_SESSION['member_info']['member_id']);
		}
		
		//delete session
		$mod_members = new Db_Tables_Members();
		$mod_members->ExitMemberFromSession();
		
		$this->_redirect("/index/dashboard");
	}
	
	function veriImageAction()
	{
		$mod = new Sys_Vericode();
		$mod->doimg();
		$_SESSION['veri-image'] = $mod->getCode();
		die;
	}
	
	function forgetPasswordAction()
	{
		$params = $this->_request->getParams();
		
		if($params['cellphone'])
		{
			$cellphone = trim($params['cellphone']);
			$this->view->cellphone = $cellphone;
		}else{
			echo "非法操作";
			die;
		}
	}
	
	function forgetPasswordSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['cellphone'] && $params['passwd1'])
		{
			$cellphone = trim($params['cellphone']);
			$passwd1 = trim($params['passwd1']);
			
			$mod_members = new Db_Tables_Members();
			$row = $mod_members->fetchRow("cellphone='".$cellphone."'");
			
			if($row)
			{
				$row->password = sha1($passwd1);
				
				if($row->save())
				{
					$mod_members->member_id = $row->member_id;
					$mod_members->LoadMemberInfoToSession();

					if($_SESSION['member_info']['open_id'])
					{
						$mod_wechat = new Db_Tables_MembersWechat();
						$mod_wechat->member_id = $row->member_id;
						$mod_wechat->open_id = $_SESSION['member_info']['open_id'];
						$mod_wechat->AddRecord();
					}

					$this->_redirect("/members/index");
				}else{
					echo "数据保存失败";
					die;
				}
			}else{
				echo "找不到会员";
				die;
			}
		}else{
			echo "关键参数缺失";
			die;
		}
	}
}